﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal enum DocumentType
    {
        Undefined,
        Server,
        Database,
        Tables,
        Table,
        Views,
        View,
        StoredProcedures,
        StoredProcedure,
        Functions,
        Function,
        Columns,
        Parameters,
        Schema,
        Schemas,
        Index,
        Indexes,
        Trigger,
        Triggers,
        DataTypes,
        DefaultValues,
        Keys,
        
        Diagrams,
        Documents,
        Images,
        Storage,
        Users,
        Roles,
        XmlSchemas,
        Rules,
        FulltextCatalogs,
        Synonyms,
        Partitions,
        Assemblies
    }

    internal abstract class Document
    {
        #region internal
        //Constructor
        internal Document()
        {
            ShouldDocument = false;
        }
        //Properties
        internal TreeNode TreeNode { get; set; }
        
        internal Boolean ShowHeader { get; set; }
        internal Html.LeftNav LeftNav { get; set; }

        /// <summary>
        /// This is the name that is displayed on the UI element for this document
        /// </summary>
        internal abstract String DisplayName { get; }
        /// <summary>
        /// This represents the type of Html page that this document contains
        /// </summary>
        internal DocumentType DocumentType { get; set; }
        /// <summary>
        /// This is the URL that will be output to the file system.  
        /// It should be unique for each HtmlDocument under a given server.
        /// </summary>
        internal string URL { get; set; }
        /// <summary>
        /// This is the folder path that the executable is currently running under.
        /// It's used to provide accurate HRef links from within the document
        /// </summary>
        internal static string RootFolderPath { get; set; }
        /// <summary>
        /// This is a short cut to the Server object.
        /// </summary>
        internal SqlServer.Server RootServer { get; set; }
        /// <summary>
        /// This is a short cut to the Database object.
        /// </summary>
        internal SqlServer.Database RootDatabase { get; set; }
        internal Boolean ShouldDocument { get; set; }

        //Methods
        /// <summary>
        /// Returns the HTML for this document
        /// </summary>
        /// <returns>String</returns>
        internal abstract String GetHtml();
        /// <summary>
        /// This clears all of the Html, Css, and JavaScript for this document. 
        /// The next time GetHtml is called it will reload everything.
        /// </summary>
        internal void Invalidate()
        {
            _body = new StringBuilder();
            _mainCss = new StringBuilder();
            _customCss = new StringBuilder();
            _mainJavaScript = new StringBuilder();
            _customJavaScript = new StringBuilder();
            _header = new StringBuilder();
            _html = "";
        }
        #endregion
        #region Protected
        //Properties
        protected string _html = "";
        //Methods
        /// <summary>
        /// Adds a line of Html code to the body of this document
        /// </summary>
        /// <param name="bodyLine"></param>
        protected void AddBodyLine(string bodyLine)
        {
            AddBodyLine(bodyLine, 0);
        }
        protected void AddBodyLine(string bodyLine, int numOfTabs)
        {
            for (int i = 0; i < numOfTabs; i++)
            {
                _body.Append("\t");
            }
            _body.Append(bodyLine + EOL);
        }
        /// <summary>
        /// Adds a line of CSS code to this document
        /// </summary>
        /// <param name="cssLine"></param>
        protected void AddCssLine(string cssLine)
        {
            AddCssLine(cssLine, 0);
        }
        protected void AddCssLine(string cssLine, int numOfTabs)
        {
            for (int i = 0; i < numOfTabs; i++)
            {
                _customCss.Append("\t");
            }
            _customCss.Append(cssLine + EOL);
        }
        /// <summary>
        /// Adds a line of JavaScript code to this document
        /// </summary>
        /// <param name="javaScriptLine"></param>
        protected void AddJavaScriptLine(string javaScriptLine)
        {
            AddJavaScriptLine(javaScriptLine, 0);
        }
        protected void AddJavaScriptLine(string javaScriptLine, int numOfTabs)
        {
            for (int i = 0; i < numOfTabs; i++)
            {
                _customJavaScript.Append("\t");
            }
            _customJavaScript.Append(javaScriptLine + EOL);
        }
        /// <summary>
        /// This creates the Html code for the subclass so that they can return it 
        /// to clients via the GetHtml method
        /// </summary>
        /// <returns></returns>
        protected string CreateHtmlDocument()
        {
            LoadHtmlHeader();
            LoadJavaScript();
            LoadCss();

            StringBuilder sb = new StringBuilder();
            sb.Append(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">");
            
            sb.Append(@"<html>" + EOL);
            sb.Append(@"<head>" + EOL);
            sb.Append(@"<meta http-equiv=""Content-type"" content=""text/html;charset=UTF-8""> " + EOL);
            sb.Append(@"<title>" + EOL);
            sb.Append(@"</title>" + EOL);

            sb.Append(@"<script type=""text/javascript"" language=""JavaScript"">" + EOL);
            sb.Append(_mainJavaScript.ToString() + EOL);
            sb.Append(_customJavaScript.ToString() + EOL);
            sb.Append(@"</script>" + EOL);

            sb.Append(@"<style TYPE=""text/css"">" + EOL + @"<!--" + EOL);
            sb.Append(_mainCss.ToString());
            sb.Append(_customCss.ToString());
            sb.Append(@"-->" + EOL + @"</style>" + EOL);

            sb.Append(@"</head>" + EOL);
            sb.Append(@"<body>" + EOL);

            sb.Append(_header.ToString());
            sb.Append(_body.ToString());

            sb.Append(GetHtmlFooter());

            sb.Append(@"</body>" + EOL);
            sb.Append(@"</html>" + EOL);
            
            return sb.ToString();
        }
        
        //Supported Methods
        /// <summary>
        /// Takes a numeric string and adds commas between every 3rd character
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        protected string AddCommasToNumericString(string source)
        {
            string returnString = "";
            while (source.Length > 3)
            {
                returnString += "," + source.Substring(source.Length - 3, 3);
                source = source.Substring(0, source.Length - 3);
            }
            returnString = source + returnString;
            return returnString;
        }
        /// <summary>
        /// Takes a string and adds wrapps it with a Html <H2> ag
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        protected string H2(string source) { return @"<h2>" + source + @"</h2>"; }
        /// <summary>
        /// Takes a integer and depending on what the system type is returns
        /// the actual maximum length.  Ex. Unicode types are divided by 2
        /// </summary>
        /// <param name="length"></param>
        /// <param name="systemType"></param>
        /// <returns></returns>
        protected int ActualMaxLength(int length, SqlServer.SystemType systemType)
        {
            if (systemType.Category == DataCoreTechnologies.SqlServer.SystemTypeCategory.UnicodeCharacterStrings)
                return length / 2;
            else
                return length;
        }
        /// <summary>
        /// Replaces invalid HTML characters with their encoded equivalants
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        protected string ScrubHtml(string source)
        {
            if (source != null)
            {
                source = source.Replace("&", "&amp;");
                source = source.Replace("<", @"&lt;");
                source = source.Replace(">", @"&gt;");
                source = source.Replace(@"""", @"&quot;");
                source = source.Replace(@"'", @"&#39;");
                source = source.Replace(" ", @"&nbsp; ");
            }
            return source;
        }
        protected string ScrubDDL(string source)
        {
            if (source != null)
            {
                source = source.Replace("&", "&amp;");
                source = source.Replace("<", @"&lt;");
                source = source.Replace(">", @"&gt;");
                source = source.Replace(@"""", @"&quot;");
                source = source.Replace(@"'", @"&#39;");
                source = source.Replace(" ", @"&nbsp;");
            }
            return source;
        }
        protected List<string> ColorizeDDL(List<string> tokens)
        {
            #region Colorize
            var blueWords = new List<string>() { "add", "alter", "as", "asc", "authorization", "backup", "begin",
                            "binary", "bit", "break", "browse", "bulk", "by", "cascade", "case", "catch", "char", "check",
                            "checkpoint", "close", "clustered", "collate", "column", "commit", "compute",
                            "constraint", "contains", "containstable", "continue", "create", "current",
                            "current_date", "current_time", "current_timestamp", "current_user", "cursor", "database",     
                            "date", "datetime", "dbcc", "deallocate", "decimal", "declare", "default", "delete", "deny", "desc",
                            "disk", "distinct", "distributed", "drop", "else", "end", "end-exec", "errlvl", "escape",
                            "except", "exec", "execute", "exit", "external", "fetch", "file", "fillfactor", "float",
                            "for", "foreign", "freetext", "freetexttable", "from", "full", "function", "goto", "grant",
                            "group", "having", "holdlock", "identity", "identity_insert", "identitycol", "if", "index",
                            "insert", "int", "intersect", "into", "key", "kill", "lineno", "millisecond", "national", 
                            "nchar", "nocheck", "nocount",
                            "nonclustered", "ntext", "nullif", "numeric", "of", "off", "offsets", "on", "open", 
                            "opendatasource", "openquery", "openrowset", "openxml", "option", "order", "over", "parameter",
                            "percent", "plan", "precision", "primary", "print", "proc", "procedure", "public", "raiserror",
                            "read", "readtext", "real", "reconfigure", "references", "replication", "restore", "restrict",
                            "return", "returns", "revoke", "rollback", "rowcount", "rowguidcol", "rule", "save", "schema", "select",
                            "session_user", "set", "setuser", "shutdown", "smallint", "statistics", "system_user",
                            "table", "textsize", "then", "timestamp", "to", "top", "tran", "transaction", "trigger",
                            "truncate", "try", "tsequal", "unique", "update", "updatetext", "use", "user", "values", "varying", "view",
                            "waitfor", "when", "where", "while", "with", "writetext" };
            var fuciaWords = new List<string>() { "avg", "cast", "coalesce", "convert", "count", "dateadd", "day", "grouping", 
                            "lower", "max", "min", 
                            "month", "space", "substring", "sum", "upper", "year", 
                            "@@connections", "@@cpu_busy", "@@cursor_rows", "@@datefirst", "@@dbts", "@@error", "@@fetch_status",
                            "@@identity", "@@idle", "@@io_busy", "@@langid", "@@language", "@@lock_timeout", "@@max_connections",
                            "@@max_precision", "@@nestlevel", "@@options", "@@pack_received", "@@pack_sent", "@@packet_errors",
                            "@@procid", "@@remserver", "@@rowcount", "@@servername", "@@servicename", "@@spid", "@@textsize",
                            "@@timeticks", "@@total_errors", "@@total_read", "@@total_write", "@@trancount", "@@version" };
            var grayWords = new List<string>() { "all", "and", "any", "between", "cross", "exists", "in", "inner", 
                            "is", "join", "left", "like", "not", "null", "or", "outer", "right", "union" };
            var coloredList = new List<string>();
            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i].StartsWith("--")) coloredList.Add(@"<span class=""ddlGreen"">" + tokens[i] + @"</span>");
                else if (tokens[i].StartsWith("/*"))
                {
                    string keyword = tokens[i];
                    string code = "";
                    for (int j = 0; j < keyword.Length; j++)
                    {
                        if (keyword.Substring(j, 2) == "\r\n")
                        {
                            coloredList.Add(@"<span class=""ddlGreen"">" + ScrubDDL(code) + @"</span>");
                            coloredList.Add("\r\n");
                            j++;
                            code = "";
                        }
                        else
                        {
                            if (j == keyword.Length - 2)
                            {
                                coloredList.Add(@"<span class=""ddlGreen"">" + ScrubDDL(code + keyword.Substring(j, 2)) + @"</span>");
                                j++;
                            }
                            else
                                code += keyword.Substring(j, 1);
                        }
                    }
                }
                else if (tokens[i].StartsWith("'"))
                {
                    string keyword = tokens[i];
                    string code = "";
                    for (int j = 0; j < keyword.Length; j++)
                    {
                        if (keyword.Substring(j, 2) == "\r\n")
                        {
                            coloredList.Add(@"<span class=""ddlRed"">" + ScrubDDL(code) + @"</span>");
                            coloredList.Add("\r\n");
                            j++;
                            code = "";
                        }
                        else
                        {
                            if (j == keyword.Length - 2)
                            {
                                coloredList.Add(@"<span class=""ddlRed"">" + ScrubDDL(code + keyword.Substring(j, 2)) + @"</span>");
                                j++;
                            }
                            else
                                code += keyword.Substring(j, 1);
                        }
                    }
                }
                else if (blueWords.Contains(tokens[i].ToLower())) coloredList.Add(@"<span class=""ddlBlue"">" + ScrubDDL(tokens[i]) + @"</span>");
                else if (fuciaWords.Contains(tokens[i].ToLower())) coloredList.Add(@"<span class=""ddlFucia"">" + ScrubDDL(tokens[i]) + @"</span>");
                else if (grayWords.Contains(tokens[i].ToLower())) coloredList.Add(@"<span class=""ddlGray"">" + ScrubDDL(tokens[i]) + @"</span>");
                else coloredList.Add(ScrubDDL(tokens[i]));
            }
            #endregion
            #region Recombine
            var output = new List<string>();
            for (int i = 0; i < coloredList.Count; i++)
            {
                string code = "";
                while (i < coloredList.Count
                && coloredList[i].EndsWith("\r\n") == false)
                {
                    code += coloredList[i];
                    i++;
                }
                output.Add(code);
            }
            #endregion
            return output;
        }
        protected string DisplayParameter(SqlServer.Parameter parameter)
        {
            string returnValue = "";
            if (parameter.UserType == null)
            {
                returnValue = parameter.SystemType.Name;
                if (parameter.SystemType.Category == SqlServer.SystemTypeCategory.CharacterStrings)
                    returnValue += @"(" + parameter.Length.ToString() + @")";
                else if (parameter.SystemType.Category == SqlServer.SystemTypeCategory.UnicodeCharacterStrings)
                    returnValue += @"(" + (parameter.Length / 2).ToString() + @")";
                else if (parameter.SystemType.Category == SqlServer.SystemTypeCategory.ExactNumerics &&
                    (parameter.SystemType.Name == "numeric" || parameter.SystemType.Name == "decimal"))
                    returnValue += @"(" + parameter.Precision.ToString() + "," + parameter.Scale.ToString() + ")";
            }
            else
            {
                returnValue = parameter.UserType.Name;
                if (parameter.UserType.SystemType.Category == SqlServer.SystemTypeCategory.CharacterStrings)
                    returnValue += " (" + parameter.UserType.SystemType.Name + "(" + parameter.Length.ToString() + @"))";
                else if (parameter.UserType.SystemType.Category == SqlServer.SystemTypeCategory.UnicodeCharacterStrings)
                    returnValue += " (" + parameter.UserType.SystemType.Name + "(" + (parameter.Length / 2).ToString() + @"))";
                else if (parameter.SystemType.Category == SqlServer.SystemTypeCategory.ExactNumerics &&
                    (parameter.SystemType.Name == "numeric" || parameter.SystemType.Name == "decimal"))
                    returnValue += " (" + parameter.UserType.SystemType.Name + "(" + parameter.Precision.ToString() + "," + parameter.Scale.ToString() + "))";
                else
                    returnValue += " (" + parameter.UserType.SystemType.Name + ")";
            }

            return returnValue;
        }
        protected string DisplayColumn(SqlServer.Column column)
        {
            string returnValue = "";
            if (column.UserType == null) 
            {
                returnValue = column.SystemType.Name;
                if (column.SystemType.Category == SqlServer.SystemTypeCategory.CharacterStrings)
                    returnValue += @"(" + column.MaxLength.ToString() + @")";
                else if (column.SystemType.Category == SqlServer.SystemTypeCategory.UnicodeCharacterStrings)
                    returnValue += @"(" + (column.MaxLength / 2).ToString() + @")";
                else if (column.SystemType.Category == SqlServer.SystemTypeCategory.ExactNumerics &&
                    (column.SystemType.Name == "numeric" || column.SystemType.Name == "decimal"))
                    returnValue += @"(" + column.Precision.ToString() + "," + column.Scale.ToString() + ")";
            }
            else 
            {
                returnValue = column.UserType.Name;
                if (column.UserType.SystemType.Category == SqlServer.SystemTypeCategory.CharacterStrings)
                    returnValue += " (" + column.UserType.SystemType.Name + "(" + column.MaxLength.ToString() + @"))";
                else if (column.UserType.SystemType.Category == SqlServer.SystemTypeCategory.UnicodeCharacterStrings)
                    returnValue += " (" + column.UserType.SystemType.Name + "(" + (column.MaxLength / 2).ToString() + @"))";
                else if (column.SystemType.Category == SqlServer.SystemTypeCategory.ExactNumerics && 
                    (column.SystemType.Name == "numeric" || column.SystemType.Name == "decimal"))
                    returnValue += " (" + column.UserType.SystemType.Name + "(" + column.Precision.ToString() + "," + column.Scale.ToString() + "))";
                else
                    returnValue += " (" + column.UserType.SystemType.Name + ")";
            }

            return returnValue;
        }
        protected void AddDdlCssColors()
        {
            _mainCss.Append(@"      #ddl *.sqlCode td {background-color:white; color:black; font-family:courier; border-style:none; }" + EOL);
            _mainCss.Append(@"      #ddl *.ddlRed {color: #FF0000; font-family:courier; };" + EOL);
            _mainCss.Append(@"      #ddl *.ddlFucia {color: #FF00FF; font-family:courier; };" + EOL);
            _mainCss.Append(@"      #ddl *.ddlBlue {color: #0000FF; font-family:courier; };" + EOL);
            _mainCss.Append(@"      #ddl *.ddlGreen {color: #008000; font-family:courier; };" + EOL);
            _mainCss.Append(@"      #ddl *.ddlGray {color: #808080; font-family:courier; };" + EOL);
            _mainCss.Append(EOL);
        }
        #endregion
        #region Private
        //Properties
        private string EOL { get { return "\r\n"; } }
        private StringBuilder _body = new StringBuilder();
        private StringBuilder _mainCss = new StringBuilder();
        private StringBuilder _customCss = new StringBuilder();
        private StringBuilder _mainJavaScript = new StringBuilder();
        private StringBuilder _customJavaScript = new StringBuilder();
        private StringBuilder _header = new StringBuilder();

        //Methods
        /// <summary>
        /// Returns the code to create the header portion of the Html
        /// </summary>
        /// <returns></returns>
        private void LoadHtmlHeader()
        {
            string href = "";

            if (DocumentType == DocumentType.Server)
            {
                var databases = (from database in RootServer.Databases
                                 where database.IsInformationLoaded == true
                                 orderby database.Name
                                 select database);

                _header.Append(@"<div id=""topMenu"">" + EOL);
                if (databases.Count() != 0)
                {
                    _header.Append(@"<ul>" + EOL);
                    foreach (SqlServer.Database database in databases)
                    {
                        href = @"<a href=""http://index." +
                            database.Name + @".html"">" + ScrubHtml(database.Name) + @"</a>";
                        _header.Append(@"<li>" + href + @"</li>" + EOL);
                    }
                    _header.Append(@"</ul>" + EOL);
                }
                _header.Append(@"</div>" + EOL);
                _header.Append(@"<div class=""headerLine"">&nbsp;</div>" + EOL);
            }
            else
            {
                _header.Append(@"<div id=""topMenu"">" + EOL);
                _header.Append(@"<ul>" + EOL);

                _header.Append(@"<li><a href=""http://index.html"">Home</a></li>" + EOL);

                href = @"<a href=""http://index." + ScrubHtml(RootDatabase.Name) + @".html"">" +
                    ScrubHtml(RootDatabase.Name) + @"</a>";
                if (DocumentType == DocumentType.Database)
                    _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                else
                    _header.Append(@"<li>" + href + @"</li>" + EOL);

                _header.Append(@"</ul>" + EOL);
                _header.Append(@"</div>" + EOL);
                _header.Append(@"<div class=""headerLine"">&nbsp;</div>" + EOL);

                _header.Append(@"<div id=""topMenu2"">" + EOL);
                _header.Append(@"<ul>" + EOL);

                href = @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">Schemas</a>";
                if (DocumentType == DocumentType.Schemas | DocumentType == DocumentType.Schemas)
                    _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                else
                    _header.Append(@"<li>" + href + @"</li>" + EOL);

                if (Business.FilterPreferences.ShowTablesPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".tables.html"">Tables</a>";
                   if (DocumentType == DocumentType.Tables | DocumentType == DocumentType.Table)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowViewsPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".views.html"">Views</a>";
                   if (DocumentType == DocumentType.Views | DocumentType == DocumentType.View)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowStoredProceduresPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".storedprocedures.html"">Stored Procedures</a>";
                   if (DocumentType == DocumentType.StoredProcedures | DocumentType == DocumentType.StoredProcedure)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowFunctionsPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".functions.html"">Functions</a>";
                   if (DocumentType == DocumentType.Functions | DocumentType == DocumentType.Function)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);

                }

                _header.Append(@"</ul>" + EOL);
                _header.Append(@"</div>" + EOL);
                _header.Append(@"<div class=""headerLine"">&nbsp;</div>" + EOL);

                _header.Append(@"<div id=""topMenu3"">" + EOL);
                _header.Append(@"<ul>" + EOL);

                if (Business.FilterPreferences.ShowTriggersPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".triggers.html"">Triggers</a>";
                   if (DocumentType == DocumentType.Triggers | DocumentType == DocumentType.Trigger)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowIndexesPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".indexes.html"">Indexes</a>";
                   if (DocumentType == DocumentType.Indexes | DocumentType == DocumentType.Index)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);

                }
               
                if (Business.FilterPreferences.ShowDefaultValuesPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".defaultvalues.html"">Default Values</a>";
                   if (DocumentType == DocumentType.DefaultValues)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowColumnsPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".columns.html"">Columns</a>";
                   if (DocumentType == DocumentType.Columns)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowParametersPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".parameters.html"">Parameters</a>";
                   if (DocumentType == DocumentType.Parameters)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                if (Business.FilterPreferences.ShowDataTypesPage)
                {
                   href = @"<a href=""http://index." + RootDatabase.Name + @".datatypes.html"">Data Types</a>";
                   if (DocumentType == DocumentType.DataTypes)
                      _header.Append(@"<li id=""selectedMenuItem"">" + href + @"</li>" + EOL);
                   else
                      _header.Append(@"<li>" + href + @"</li>" + EOL);
                }

                _header.Append(@"</ul>" + EOL);
                _header.Append(@"</div>" + EOL);
                _header.Append(@"<div class=""headerLine"">&nbsp;</div>" + EOL);
            }
        }
        private void LoadCss()
        {
            string cssLine = "";

            _mainCss.Append(@"      * { font-family:sans-serif; color: #" +
               ColorPreferences.DefaultHexString + @";}" + EOL);
            _mainCss.Append(@"      #topNav { font-size: 14px; }" + EOL);
            _mainCss.Append(@"      #title { font-size: 26px; }" + EOL);
            _mainCss.Append(EOL);
            _mainCss.Append(@"      #topMenu, #topMenu2, #topMenu3 { width: 100%; background-color: white}" + EOL);
            _mainCss.Append(@"      #topMenu ul, #topMenu2 ul, #topMenu3 ul { list-style:none; margin: 0px; padding: 0px;}" + EOL);
            _mainCss.Append(@"      #topMenu li, #topMenu2 li, #topMenu3 li { float:left; margin: 10px; background-color: #" +
               ColorPreferences.DefaultHexString + @";}" + EOL);
            _mainCss.Append(@"      #topMenu a, #topMenu2 a, #topMenu3 a { color:white; padding-left: 6px; padding-right: 6px; font-weight:bold;}" + EOL);
            _mainCss.Append(@"      .headerLine { clear:both; width: 100%; background-color: #" +
               ColorPreferences.DefaultHexString + @"; font-size:5px; margin-left:0px;}" + EOL);
            _mainCss.Append(@"      #topMenu ul:hover, #topMenu2 ul:hover, #topMenu3 ul:hover, " + EOL);
            _mainCss.Append(@"      #topMenu a:hover, #topMenu2 a:hover, #topMenu3 a:hover { background-color: #" +
               ColorPreferences.HighlightedHexString + @"; }" + EOL);
            _mainCss.Append(@"      #selectedMenuItem a { background-color: #" +
               ColorPreferences.HighlightedHexString + @"; }" + EOL);
            _mainCss.Append(EOL);
            _mainCss.Append(@"      #mainBody { clear: both; padding-top: 10px; padding-left: 0px; padding-bottom: 10px; word-wrap: break-word;}" + EOL);
            _mainCss.Append(@"      #mainBody table { color: #" +
               ColorPreferences.DefaultHexString + @"; border-collapse: collapse; border-style: solid; border-width: 1px; border-color: black; width: 100%}" + EOL);
            _mainCss.Append(@"      #mainBody th {color: white; background-color: #" +
               ColorPreferences.DefaultHexString + @"; text-align: left;}" + EOL);
            _mainCss.Append(@"      #mainBody td, th { border-style: solid; border-width: 1px; border-color: black; padding-left:5px; padding-right:5px;}" + EOL);
            
            cssLine = @"      #mainBody *.evenRow { background-color:#" +
                ColorPreferences.EvenRowHexString + @";}";
            _mainCss.Append(cssLine + EOL);

            _mainCss.Append(EOL);
            _mainCss.Append(@"      #footerDiv { font-size: 14px;}" + EOL);
            _mainCss.Append(@"      #footerDiv { text-align: center; padding-top: 10px;}" + EOL);
            _mainCss.Append(@"      #footerDiv a:link, a:visited, a:active {text-decoration: none; color: #" +
               ColorPreferences.DefaultHexString + @"; font-weight: bold; }" + EOL);
            _mainCss.Append(@"      #footerDiv a:hover {text-decoration: underline; color: #" +
               ColorPreferences.DefaultHexString + @"; font-weight: bold;}" + EOL);
            _mainCss.Append(EOL);
        }
        private void LoadJavaScript()
        {
            _mainJavaScript.Append(@"function toggleDiv(divId){" + EOL);
            _mainJavaScript.Append(@"    var imgLoc = ""img_"" + divId;" + EOL);
            _mainJavaScript.Append(@"    image = document.getElementById(imgLoc);" + EOL);
            _mainJavaScript.Append(@"    var imgLoc = ""img_"" + divId;" + EOL);
            _mainJavaScript.Append(@"    if(document.getElementById(divId).style.display == 'none'){" + EOL);
            _mainJavaScript.Append(@"        document.getElementById(divId).style.display = 'block';" + EOL);
            _mainJavaScript.Append(@"        image.src = '" + RootFolderPath + @"minus.jpg';" + EOL);
            _mainJavaScript.Append(@"    }else{" + EOL);
            _mainJavaScript.Append(@"        document.getElementById(divId).style.display = 'none';" + EOL);
            _mainJavaScript.Append(@"        image.src = '" + RootFolderPath + @"plus.jpg';" + EOL);
            _mainJavaScript.Append(@"    }" + EOL);
            _mainJavaScript.Append(@"}" + EOL);
        }
        /// <summary>
        /// Returns the Html code for the footer of this document
        /// </summary>
        /// <returns></returns>
        private string GetHtmlFooter()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(@"<div id=""footerDiv"">");
            if (RootDatabase != null)
                sb.Append(@"Loaded on : " + RootDatabase.LoadedOn.ToString() + "<br><br>\r\n");
            else
                sb.Append(@"Loaded on : " + RootServer.LoadedOn.ToString() + "<br><br>\r\n");
            sb.Append(@"<a href=""http://www.datacoretechnologies.com"">");
            sb.Append(@"Powered by Scribe &copy; 2009 DataCore Technologies</a>");
            sb.Append(@"</div>");

            return sb.ToString();
        }
        #endregion
    }
}
